package com.nofeng.wx.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.nofeng.wx.third.EastMoneyStockApi;
import com.nofeng.wx.third.FinanceHttpApi;
import com.nofeng.wx.third.NocodeDbApi;
import com.nofeng.wx.utils.BaseUtils;
import com.nofeng.wx.utils.IndicatrixUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Calendar;

@Slf4j
@Service
public class FindStockService {
    public static String ETF500="000009,000021,000027,000032,000034,000039,000050,000060,000062,000066,000155,000400,000403,000415,000423,000426,000429,000513,000519,000528,000537,000539,000543,000559,000563,000582,000591,000598,000623,000629,000636,000683,000703,000709,000723,000728,000729,000733,000738,000739,000750,000783,000785,000825,000830,000831,000878,000883,000887,000893,000898,000921,000932,000933,000937,000951,000958,000959,000960,000967,000987,000988,000997,001213,001286,001389,001696,002007,002008,002025,002044,002056,002064,002065,002078,002080,002085,002120,002130,002138,002152,002153,002155,002156,002185,002195,002202,002203,002223,002244,002261,002262,002266,002273,002281,002294,002299,002312,002318,002340,002353,002372,002385,002409,002410,002414,002423,002429,002430,002432,002436,002439,002444,002461,002465,002472,002500,002507,002508,002517,002532,002558,002563,002568,002572,002595,002603,002607,002608,002624,002653,002670,002673,002683,002738,002739,002756,002773,002797,002821,002831,002837,002841,002850,002851,002926,002939,002945,002958,002966,002984,003022,003031,003035,300001,300002,300003,300012,300017,300024,300037,300054,300058,300070,300073,300115,300136,300140,300142,300144,300146,300207,300212,300223,300251,300285,300339,300346,300373,300383,300390,300395,300450,300454,300458,300474,300476,300487,300496,300529,300558,300567,300573,300595,300601,300604,300623,300627,300676,300677,300679,300699,300724,300735,300748,300751,300757,300763,300765,300803,300857,300888,300919,300957,301165,301267,301301,301308,301358,301498,301536,600004,600007,600008,600021,600032,600038,600056,600060,600062,600079,600095,600096,600098,600109,600118,600126,600131,600132,600141,600143,600153,600155,600157,600166,600170,600171,600177,600208,600258,600271,600282,600295,600298,600299,600312,600316,600325,600329,600339,600348,600352,600361,600363,600369,600373,600378,600380,600390,600392,600398,600435,600483,600486,600487,600497,600498,600499,600511,600516,600517,600521,600528,600529,600535,600536,600546,600549,600562,600563,600566,600578,600580,600582,600583,600598,600606,600612,600637,600655,600663,600673,600685,600688,600699,600704,600707,600720,600737,600739,600754,600763,600764,600765,600801,600808,600816,600820,600839,600848,600859,600862,600863,600867,600871,600872,600873,600879,600884,600885,600895,600901,600906,600909,600925,600927,600928,600956,600959,600968,600970,600977,600985,600988,600995,600998,601000,601001,601016,601019,601061,601098,601099,601106,601108,601118,601128,601139,601155,601156,601158,601162,601168,601179,601198,601212,601216,601228,601231,601233,601333,601399,601456,601555,601568,601577,601598,601608,601611,601615,601636,601665,601666,601696,601717,601858,601866,601880,601918,601921,601928,601933,601958,601965,601966,601990,601991,601997,603000,603077,603087,603129,603156,603160,603179,603225,603228,603233,603290,603298,603338,603341,603345,603379,603486,603529,603556,603565,603568,603589,603596,603605,603606,603650,603658,603659,603688,603707,603728,603737,603786,603816,603826,603858,603868,603885,603893,603899,603927,603939,603979,605358,605589,688002,688017,688027,688032,688052,688065,688072,688099,688100,688114,688120,688122,688153,688172,688180,688183,688188,688213,688220,688234,688235,688248,688249,688278,688281,688295,688297,688301,688318,688349,688361,688363,688375,688385,688387,688425,688469,688475,688520,688521,688525,688538,688561,688563,688568,688578,688582,688608,688617,688676,688702,688728,688772,688777,688778,688819,689009";
    public static String ETF300="000001,000002,000063,000100,000157,000166,000301,000333,000338,000408,000425,000538,000568,000596,000617,000625,000630,000651,000661,000708,000725,000768,000776,000786,000792,000800,000807,000858,000876,000895,000938,000963,000975,000977,000983,000999,001289,001391,001965,001979,002001,002027,002028,002049,002050,002074,002129,002142,002179,002180,002230,002236,002241,002252,002304,002311,002352,002371,002415,002422,002459,002460,002463,002466,002475,002493,002594,002600,002601,002648,002709,002714,002736,002916,002920,002938,003816,300014,300015,300033,300059,300122,300124,300274,300308,300316,300347,300394,300408,300413,300418,300433,300442,300498,300502,300628,300661,300750,300759,300760,300782,300832,300896,300979,300999,301236,301269,302132,600000,600009,600010,600011,600015,600016,600018,600019,600023,600025,600026,600027,600028,600029,600030,600031,600036,600039,600048,600050,600061,600066,600085,600089,600104,600111,600115,600150,600160,600161,600176,600183,600188,600196,600219,600233,600276,600309,600332,600346,600362,600372,600377,600406,600415,600426,600436,600438,600460,600482,600489,600515,600519,600547,600570,600584,600585,600588,600600,600660,600674,600690,600741,600760,600795,600803,600809,600845,600875,600886,600887,600893,600900,600905,600918,600919,600926,600938,600941,600958,600989,600999,601006,601009,601012,601021,601058,601059,601066,601077,601088,601100,601111,601117,601127,601136,601138,601166,601169,601186,601211,601225,601229,601236,601238,601288,601318,601319,601328,601336,601360,601377,601390,601398,601600,601601,601607,601618,601628,601633,601658,601668,601669,601688,601689,601698,601699,601728,601766,601788,601799,601800,601808,601816,601818,601825,601838,601857,601865,601868,601872,601877,601878,601881,601888,601898,601899,601901,601916,601919,601939,601985,601988,601989,601995,601998,603019,603195,603259,603260,603288,603296,603369,603392,603501,603799,603806,603833,603986,603993,605117,605499,688008,688009,688012,688036,688041,688047,688082,688111,688126,688169,688187,688223,688256,688271,688303,688396,688472,688506,688599,688981";
    //https://www.csindex.com.cn/#/indices/family/detail?indexCode=931643
    //https://www.csindex.com.cn/#/indices/family/detail?indexCode=000688
    public static String ETF_KE="688008,688009,688012,688036,688041,688047,688065,688072,688082,688099,688111,688114,688120,688122,688126,688169,688180,688187,688188,688213,688223,688234,688249,688256,688271,688278,688297,688303,688349,688363,688375,688396,688469,688472,688475,688506,688521,688525,688538,688561,688563,688578,688599,688608,688617,688728,688777,688819,688981,689009,300014,300122,300124,300223,300274,300308,300316,300347,300394,300408,300433,300442,300450,300502,300628,300661,300750,300759,300760,300782,300832,300919,301269,301308,302132,688008,688009,688012,688036,688041,688047,688072,688082,688111,688126,688169,688187,688188,688223,688256,688271,688303,688349,688396,688472,688506,688599,688728,688777,688981";

    public static Set<String> supperETF=Sets.newHashSet("510300","510500","159915","518880","159941","588000","513050","512100");
    @Autowired
    private NocodeDbApi nocodeDbApi;
    @Autowired
    private StockService stockService;

    public void saveStrongStock(){
        for (String etf : supperETF) {
            checkETF(etf);
        }
        List<JSONObject> jsonObjectList = findStrongStock();
        try {
            refreshFundFlow();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashSet<String> etf500Set = Sets.newHashSet(ETF500.split(","));
        HashSet<String> etf300Set = Sets.newHashSet(ETF300.split(","));
        HashSet<String> etfkeSet = Sets.newHashSet(ETF_KE.split(","));
        HashSet<String> codeSet=Sets.newHashSet();
        for (JSONObject jsonObject : jsonObjectList) {
            Map<String,Object> map=Maps.newHashMap();
            String code = jsonObject.getString("f12");
            if(etf500Set.contains(code)){
                code+="(500ETF)";
            }
            if(etf300Set.contains(code)){
                code+="(300ETF)";
            }
            if(etfkeSet.contains(code)){
                code+="(科创+创业)";
            }
            map.put("code", code);
            if(codeSet.contains(code)){
                continue;
            }else {
                codeSet.add(code);
            }
            map.put("buy_price",jsonObject.getString("f2"));
            map.put("stock_name",jsonObject.getString("f14"));
            map.put("change",jsonObject.getString("f3"));
            map.put("date",BaseUtils.getDate(0));
            map.put("sector",jsonObject.getString("sector"));
            map.put("stop_loss",jsonObject.getString("ma20"));
            map.put("profit","0");
            Boolean inserted = nocodeDbApi.insert("strong_stock", "strong_stock", JSONObject.toJSONString(map));
            System.out.println(inserted);
        }

    }

    private void checkETF(String code) {
        JSONArray stockHistory = StockService.getStockHistory(code);
        boolean needBuy300=false;
        JSONObject y1 = stockHistory.getJSONObject(stockHistory.size()-3);
        JSONObject y2 = stockHistory.getJSONObject(stockHistory.size()-2);
        JSONObject y = stockHistory.getJSONObject(stockHistory.size()-1);
        Double atr21pt = y.getDouble("atr21pt");
        if(y.getDouble("chp")>0 && y1.getDouble("chp")<0 && y2.getDouble("chp")<0){
            if(y1.getDouble("chp")+y2.getDouble("chp")<-atr21pt*4){
                if(y1.getDouble("rsi")<15 ||y2.getDouble("rsi")<15)
                needBuy300=true;
            }
        }

        if(needBuy300){
            Map<String,Object> map=Maps.newHashMap();
            map.put("code", code);
            map.put("buy_price",y.getString("close"));
            map.put("stock_name",code+"【强制必买】");
            map.put("change","0.00");
            map.put("date",BaseUtils.getDate(0));
            map.put("sector","0");
            map.put("stop_loss",y1.getString("close"));
            map.put("profit","0");
            Boolean inserted = nocodeDbApi.insert("strong_stock", "strong_stock", JSONObject.toJSONString(map));
        }
    }

    public void refresh(){
        try{
            String result = nocodeDbApi.query("strong_stock", "strong_stock", "?select=*&order=created_at.desc");
            JSONArray jsonArray = JSONArray.parseArray(result);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String codeValue = jsonObject.getString("code");
                if(supperETF.contains(codeValue)){
                    continue;
                }
                //created_at -> 2025-07-23T17:38:02.247673+08:00
                String createdAt = jsonObject.getString("created_at");
                createdAt=StringUtils.replace(createdAt,"-","");
                createdAt=StringUtils.substringBefore(createdAt,"T");
                String code = StringUtils.substringBefore(codeValue,"(");
                JSONArray stockK = EastMoneyStockApi.getStockK(code,BaseUtils.getDate(-50).toString());
                //计算持仓后的最高价位
                List<BigDecimal> maxList=Lists.newArrayList();
                List<BigDecimal> maxCloseList=Lists.newArrayList();
                for (int j = 0; j < stockK.size(); j++) {
                    String stockKString = stockK.getString(j);
                    String[] kstockDetails = StringUtils.split(stockKString,",");
                    String kTime=StringUtils.replace(kstockDetails[0],"-","");
                    kTime=StringUtils.substringBefore(kTime,"T");
                    if(BaseUtils.stringToInteger(kTime)>BaseUtils.stringToInteger(createdAt)){
                        maxList.add(BaseUtils.stringToBigDecimal(kstockDetails[3]));
                        maxCloseList.add(BaseUtils.stringToBigDecimal(kstockDetails[2]));
                    }
                }
                if(CollectionUtils.isEmpty(maxList)){
                    continue;
                }
                BigDecimal max = IndicatrixUtils.max(maxList);
                BigDecimal maxClose = IndicatrixUtils.max(maxCloseList);

                String[] stockDetails = StringUtils.split(stockK.getString(stockK.size()-1),",");
                String now = stockDetails[2];
                Double buyPrice = jsonObject.getDouble("buy_price");
                Integer date = jsonObject.getInteger("date");

                String where="?id=eq."+jsonObject.getInteger("id");
                Map<String,Object> map=Maps.newHashMap();
                List<String> tags=Lists.newArrayList();
                Double dbSellPrice = jsonObject.getDouble("sell_price");
                double nowPriceDouble = BaseUtils.stringToBigDecimal(now).doubleValue();
                if(null== dbSellPrice ) {
                    if (BaseUtils.getDate(0) - date >= 4 && nowPriceDouble < buyPrice) {
                        map.put("sell_price", now);
                        map.put("sell_date", BaseUtils.getDate(0));
                        tags.add("时间止损");
                        dbSellPrice = nowPriceDouble;
                    }
                    if(nowPriceDouble<max.doubleValue() && (max.doubleValue()-nowPriceDouble)/max.doubleValue()>=0.03){
                        map.put("sell_price", now);
                        map.put("sell_date", BaseUtils.getDate(0));
                        tags.add("高位回撤");
                        dbSellPrice = nowPriceDouble;
                    }
                    if(nowPriceDouble<maxClose.doubleValue() && (maxClose.doubleValue()-nowPriceDouble)/maxClose.doubleValue()>=0.02){
                        map.put("sell_price", now);
                        map.put("sell_date", BaseUtils.getDate(0));
                        tags.add("收盘回撤");
                        dbSellPrice = nowPriceDouble;
                    }
                    if (nowPriceDouble < getMa(stockK, 20).doubleValue()) {
                        map.put("sell_price", now);
                        map.put("sell_date", BaseUtils.getDate(0));
                        tags.add("破20天");
                        dbSellPrice = nowPriceDouble;
                    }
                }
                if(CollectionUtils.isNotEmpty(tags)){
                    map.put("tags",StringUtils.join(tags,","));
                }

                double cuteSellPrice=dbSellPrice!=null?dbSellPrice:nowPriceDouble;
                double v = (cuteSellPrice - buyPrice) / buyPrice*100;
                map.put("profit",v);

                Boolean inserted = nocodeDbApi.update("strong_stock", "strong_stock",where, JSONObject.toJSONString(map));
                System.out.println(inserted);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public List<JSONObject> findStrongStock(){
        JSONArray hotIndustry = null;
        List<JSONObject> stockList=Lists.newArrayList();
        try {
            hotIndustry = EastMoneyStockApi.getHotIndustry(1);
            //只检查前10
            for (int i = 0; i < 10; i++) {
            JSONObject industryJSON = hotIndustry.getJSONObject(i);
            String industryCode = industryJSON.getString("f12");
            String industryName = industryJSON.getString("f14");
            Long industryVolume=industryJSON.getLong("f62");
            if(industryVolume<18*100000000){
                continue;
            }
            JSONArray stockArray = EastMoneyStockApi.getHotIndustryDetail(industryCode);
            boolean hadPower = hadPower(stockArray, 1);
            if(hadPower){
                for (int j = 0; j < stockArray.size(); j++) {
                                        JSONObject stockJSON = stockArray.getJSONObject(j);
                    if(!stockJSON.containsKey("f3")){
                        continue;
                    }
                    if(stockJSON.getString("f3").equals("-")){
                        continue;
                    }
                    String code = stockJSON.getString("f12");
                    if(!ETF500.contains(code) && !ETF300.contains(code)&& !ETF_KE.contains(code)){
                        continue;
                    }
                    BigDecimal chp = stockJSON.getBigDecimal("f3");
                    BigDecimal pe = stockJSON.getBigDecimal("f9");

                    String name = stockJSON.getString("f14");
                    System.out.println("板块存在涨停有力的股票,正在细化："+industryName+"_"+name);
                    if(chp.doubleValue()>=0 && chp.doubleValue()<=3 && pe.doubleValue()>0 && pe.doubleValue()<100){
                        JSONArray stockK = EastMoneyStockApi.getStockK(code,"");
                        Boolean buy = isBuy(stockK,stockJSON);
                        if(buy) {
                            BigDecimal ma20 = getMa(stockK,20);
                            stockJSON.put("sector",industryName);
                            stockJSON.put("ma20",ma20);
                            stockList.add(stockJSON);
                        }
                    }
                }
            }else{
                System.out.println("板块缺失涨停："+industryName);
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stockList;
    }

    public void refreshFundFlow() throws Exception {
        JSONArray hotIndustry=EastMoneyStockApi.getHotIndustry(1);

        JSONArray hotIndustry3=EastMoneyStockApi.getHotIndustry(3);
        Map<String,JSONObject> hot3Map=Maps.newHashMap();
        Map<String,JSONObject> out3Map=Maps.newHashMap();
        for (int i = 0; i < 10; i++) {
            JSONObject industryJSON = hotIndustry3.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f267")>0) {
                hot3Map.put(name, industryJSON);
            }
        }
        for(int i=hotIndustry3.size()-1; i>=hotIndustry3.size()-10; i--){
            JSONObject industryJSON = hotIndustry3.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f267")<0) {
                out3Map.put(name,industryJSON);
            }
        }

        JSONArray hotIndustry5=EastMoneyStockApi.getHotIndustry(5);
        Map<String,JSONObject> hot5Map=Maps.newHashMap();
        Map<String,JSONObject> out5Map=Maps.newHashMap();
        for (int i = 0; i < 10; i++) {
            JSONObject industryJSON = hotIndustry5.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f164")>0) {
                hot5Map.put(name, industryJSON);
            }
        }
        for(int i=hotIndustry5.size()-1; i>=hotIndustry5.size()-10; i--){
            JSONObject industryJSON = hotIndustry5.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f164")<0) {
                out5Map.put(name,industryJSON);
            }
        }

        JSONArray hotIndustry10=EastMoneyStockApi.getHotIndustry(10);
        Map<String,JSONObject> hot10Map=Maps.newHashMap();
        Map<String,JSONObject> out10Map=Maps.newHashMap();
        for (int i = 0; i < 10; i++) {
            JSONObject industryJSON = hotIndustry10.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f174")>0) {
                hot10Map.put(name, industryJSON);
            }
        }
        for(int i=hotIndustry10.size()-1; i>=hotIndustry10.size()-10; i--){
            JSONObject industryJSON = hotIndustry10.getJSONObject(i);
            String name = industryJSON.getString("f14");
            if(industryJSON.getInteger("f174")<0) {
                out10Map.put(name,industryJSON);
            }
        }

        String result = nocodeDbApi.query("strong_stock", "fund_flow", "?select=*&order=created_at.desc");
        JSONArray jsonArray = JSONArray.parseArray(result);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            Boolean delete = nocodeDbApi.delete("strong_stock", "fund_flow", "?id=eq."+id);
            System.out.println(delete);
        }
        for (int i = 0; i < 10; i++) {
            JSONObject industryJSON = hotIndustry.getJSONObject(i);
            String bkCode = industryJSON.getString("f12");
            String name = industryJSON.getString("f14");
            String vol = industryJSON.getString("f62");
            double volValue = BaseUtils.stringToBigDecimal(vol).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() / 100000000;
            Map<String,Object> map=Maps.newHashMap();
            map.put("code",bkCode);
            map.put("name",name);
            map.put("sector",name);
            map.put("flow_amount",BaseUtils.format(volValue));
            map.put("flow_direction","in");
            List<String> tags=Lists.newArrayList();
            if(hot3Map.containsKey(name)){
                tags.add("3天强势");
            }
            if(hot5Map.containsKey(name)){
                tags.add("5天强势");
            }
            if(hot10Map.containsKey(name)){
                tags.add("10天强势");
            }

            if(out3Map.containsKey(name)){
                tags.add("3天弱势");
            }
            if(out5Map.containsKey(name)){
                tags.add("5天弱势");
            }
            if(out10Map.containsKey(name)){
                tags.add("10天弱势");
            }
            if(CollectionUtils.isNotEmpty(tags)){
                map.put("tags",StringUtils.join(tags,","));
            }

            Boolean inserted = nocodeDbApi.insert("strong_stock", "fund_flow", JSONObject.toJSONString(map));
            System.out.println(inserted);
        }
        for (int i = hotIndustry.size() - 1; i >= hotIndustry.size() -10; i--) {
            JSONObject industryJSON = hotIndustry.getJSONObject(i);
            String bkCode = industryJSON.getString("f12");
            String name = industryJSON.getString("f14");
            String vol = industryJSON.getString("f62");
            double volValue = BaseUtils.stringToBigDecimal(vol).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() / 100000000;
            Map<String,Object> map=Maps.newHashMap();
            map.put("code",bkCode);
            map.put("name",name);
            map.put("sector",name);
            map.put("flow_amount",BaseUtils.format(volValue));
            map.put("flow_direction","out");
            List<String> tags=Lists.newArrayList();
            if(hot3Map.containsKey(name)){
                tags.add("3天强势");
            }
            if(hot5Map.containsKey(name)){
                tags.add("5天强势");
            }
            if(hot10Map.containsKey(name)){
                tags.add("10天强势");
            }

            if(out3Map.containsKey(name)){
                tags.add("3天弱势");
            }
            if(out5Map.containsKey(name)){
                tags.add("5天弱势");
            }
            if(out10Map.containsKey(name)){
                tags.add("10天弱势");
            }
            if(CollectionUtils.isNotEmpty(tags)){
                map.put("tags",StringUtils.join(tags,","));
            }

            Boolean inserted = nocodeDbApi.insert("strong_stock", "fund_flow", JSONObject.toJSONString(map));
            System.out.println(inserted);

        }
        try {
            String stock = stockService.stock("510300", false);
            JSONObject stockJSON = JSONObject.parseObject(stock);
            FinanceHttpApi.HttpStockData  stockInfo=stockJSON.getObject("stock", FinanceHttpApi.HttpStockData.class);
            boolean above20ma=false;
            if(stockInfo.getClose().doubleValue()>=stockInfo.getMa20().doubleValue()){
                above20ma=true;
            }

            Map<String, Integer> count = EastMoneyStockApi.getCount();
            String where = "?id=eq.1";
            Map<String, Object> map = Maps.newHashMap();
            map.put("up_count", count.get("up"));
            map.put("down_count", count.get("down"));
            map.put("ratio", BaseUtils.doubleToPtStr(new BigDecimal(count.get("up")).doubleValue() / new BigDecimal(count.get("down") + count.get("up")).doubleValue()));
            map.put("above_20ma", above20ma);
            Boolean inserted = nocodeDbApi.update("strong_stock", "stock_news", where, JSONObject.toJSONString(map));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean hadPower(JSONArray stockArray,Integer powerCount){
        try {
            int power = 0;
            for (int i = 0; i < stockArray.size(); i++) {
                JSONObject stockJSON = stockArray.getJSONObject(i);
                if (stockJSON.containsKey("f3") && !stockJSON.getString("f3").equals("-")) {
                    try {
                        BigDecimal chp = stockJSON.getBigDecimal("f3");
                        if (chp.doubleValue() >= 7) {
                            power += 1;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            return power >= powerCount;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static Boolean isBuy(JSONArray stockK,JSONObject stockJSON) {
        if(stockK.size()<2){
            return false;
        }
        BigDecimal ma20 = getMa(stockK,20);
        BigDecimal ma7 = getMa(stockK,7);
        String[] stockDetails = StringUtils.split(stockK.getString(stockK.size()-1), ",");
        String[] stockDetailsBefore = StringUtils.split(stockK.getString(stockK.size()-2), ",");
        
        String now = stockDetails[2];
        String hight = stockDetails[3];
        String yesterday = stockDetailsBefore[2];
        String vol = stockDetails[6];
        String volBefore = stockDetailsBefore[6];
        String name = stockJSON.getString("f14");
        double hightDouble = BaseUtils.stringToBigDecimal(hight).doubleValue();
        boolean isMaOk=ma20.doubleValue()>ma7.doubleValue();
        boolean isUpDown=(BaseUtils.stringToBigDecimal(now).doubleValue()-hightDouble)/hightDouble<-0.01;
        boolean isAm=Calendar.getInstance().get(Calendar.HOUR_OF_DAY)<12;
        double powpt=isAm?0.8:1.8;
        
        System.out.println("正在检查"+name);
        if(isMaOk && !isUpDown &&
                BaseUtils.stringToBigDecimal(vol).doubleValue()>BaseUtils.stringToBigDecimal(volBefore).doubleValue()*powpt){
            if(BaseUtils.stringToBigDecimal(now).doubleValue()>ma20.doubleValue()
                    &&BaseUtils.stringToBigDecimal(yesterday).doubleValue()<ma20.doubleValue()
                    ){
                return true;
            }
        }
        return false;
    }

    private static BigDecimal getMa(JSONArray stockK,Integer day) {
        if(stockK.size()<day){
            return new BigDecimal(999999);
        }
        List<BigDecimal> closeList= Lists.newArrayList();
        for (int i = stockK.size() - 1; i >= stockK.size() -day; i--) {
            String[] stockDetails = StringUtils.split(stockK.getString(i), ",");
            String now = stockDetails[2];
            closeList.add(BaseUtils.stringToBigDecimal(now));
        }
        BigDecimal ma20 = IndicatrixUtils.avg(closeList);
        return ma20;
    }

}
